
package com.yami.shop.api.controller;

import com.yami.shop.bean.app.param.PayParam;
import com.yami.shop.bean.dto.WxPayVO;
import com.yami.shop.bean.pay.PayInfoDto;
import com.yami.shop.common.bean.WeChatProperties;
import com.yami.shop.security.api.model.YamiUser;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.PayService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import com.yami.shop.common.response.ServerResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 订单管理接口
 */
@RestController
@RequestMapping("/p/order")
@Tag(name = "订单接口")
@AllArgsConstructor
public class PayController {

    private final PayService payService;

    /**
     * 支付接口
     */
    @PostMapping("/pay")
    @Operation(summary = "根据订单号进行支付" , description = "根据订单号进行支付")
    public ServerResponseEntity<Void> pay(@RequestBody PayParam payParam) {
        YamiUser user = SecurityUtils.getUser();
        String userId = user.getUserId();
        PayInfoDto payInfo = payService.pay(userId, payParam);
        payService.paySuccess(payInfo.getPayNo(), "");
        return ServerResponseEntity.success();
    }

    /**
     * 微信预支付接口
     */
    @PostMapping("/transactions")
    @Operation(summary = "微信预支付" , description = "微信预支付")
    public ServerResponseEntity<WxPayVO> transactions(@RequestBody PayParam payParam)  {
        YamiUser user = SecurityUtils.getUser();
        String userId = user.getUserId();
        WxPayVO wxPayVO =null;
        try {
            wxPayVO  = payService.wxTransactionsPay(userId, payParam);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return ServerResponseEntity.success(wxPayVO);
    }
}
